package com.itheima.listener;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * @author zero
 * @date 2020/6/16
 */



@Component
public class OrderListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, com.rabbitmq.client.Channel channel) throws Exception {
        //定义deliveryTag
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //1.接收消息
            System.out.println(new String(message.getBody()));
            //2.处理业务逻辑
            System.out.println("处理业务逻辑...");
            System.out.println("1.根据订单id查询其状态...");
            System.out.println("2.判断状态是否为支付成功");
            System.out.println("3.取消订单，回滚库存....");
            //3.手动签收
            channel.basicAck(deliveryTag,true);
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("出现异常,拒绝接收");
            //4.拒绝签收
            /**
             * requeue:重回队列。如果设置为true，则消息重新回到queue，broker会重新发送该消息给消费端
             */
            channel.basicNack(deliveryTag,true,false);
        }
    }
}
